home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / SAT 2.3.8 / Demos / StepPlatform Demo ƒ / sHMovPlatform.p < prev    next >
Text File  |  1995-09-03  |  3KB  |  117 lines

  1. (*****************************************}
  2. {*    Platform sprite, moveable version, not faceless        *}
  3. {/****************************************)
  4.  
  5. unit sHMovPlatform;
  6. interface
  7.     uses
  8. {$ifc UNDEFINED THINK_PASCAL}
  9.         Types, QuickDraw, Menus, Windows, TextEdit, Fonts, Dialogs, Memory, {}
  10. {$endc}
  11.         SAT, PlatformGlobals, sPlayerSprite;
  12.  
  13.     procedure InitHMovPlatform;
  14.     procedure SetupHMovPlatform (me: SpritePtr);
  15.  
  16. implementation
  17.  
  18.     const
  19.         MovPlatformSpeed = 2;
  20.  
  21.     var
  22.         platFace: FacePtr;
  23.  
  24.     procedure InitHMovPlatform;
  25.     begin
  26.         platFace := SATGetFace(139);
  27.     end;
  28.  
  29.     procedure HandleHMovPlatform (me: SpritePtr);
  30.     begin
  31.         me^.position.h := me^.position.h + me^.speed.h;
  32.         if (me^.position.h < MovPlatP(me^.appPtr)^.MinV) then
  33.             me^.speed.h := MovPlatformSpeed;
  34.         if (me^.position.h > MovPlatP(me^.appPtr)^.MaxV) then
  35.             me^.speed.h := -MovPlatformSpeed;
  36.  
  37.         if (me^.speed.h = 0) then
  38.             begin
  39.                 if (me^.position.h > (MovPlatP(me^.appPtr)^.MaxV - MovPlatP(me^.appPtr)^.MinV) div 2) then
  40.                     me^.speed.h := -MovPlatformSpeed
  41.                 else
  42.                     me^.speed.h := MovPlatformSpeed;
  43.             end;
  44.  
  45.         me^.layer := -me^.position.v;
  46.     end;
  47.  
  48.     procedure HitHMovPlatform (me: SpritePtr; him: PlSpritePtr);
  49.         var
  50.             mini, i, min: Integer;
  51.             diff: array[0..5] of Integer;
  52.     begin
  53.         if (him^.task = @HandlePlayerSprite) then
  54.             begin
  55.                 diff[1] := -me^.hotRect2.top + (him^.hotRect2.bottom);        (* (T)toB *)
  56.                 diff[2] := -him^.hotRect2.top + (me^.hotRect2.bottom);        (* (B)toT *)
  57.                 diff[3] := -me^.hotRect2.left + (him^.hotRect2.right);            (* LtoR *)
  58.                 diff[4] := -him^.hotRect2.left + (me^.hotRect2.right);            (* RtoL *)
  59.                 mini := 0;
  60.                 min := 10000;
  61.                 for i := 1 to 4 do
  62.                     if min > diff[i] then
  63.                         begin
  64.                             min := diff[i];
  65.                             mini := i;
  66.                         end;
  67.                 case mini of
  68.                     1: (*floor*)
  69.                         begin
  70.                             him^.action := StandOnHMovPlatform;
  71.                             him^.speed.h := me^.speed.h;
  72.                             him^.position.v := him^.position.v - diff[1] + 1;
  73.                             him^.position.h := him^.position.h + me^.speed.h;
  74.                             if him^.speed.v > 0 then
  75.                                 him^.speed.h := 0;
  76.                         end;
  77.                     2: (* ceiling *)
  78.                         begin
  79.                             him^.position.v := him^.position.v + diff[2] + 1;
  80.                             if (him^.speed.v < 0) then
  81.                                 him^.speed.v := -him^.speed.v;
  82.                         end;
  83.                     3: (*left*)
  84.                         begin
  85.                             him^.position.h := him^.position.h - diff[3] - 1;
  86.                             if him^.speed.h > 0 then
  87.                                 him^.speed.h := -him^.speed.h;
  88.                         end;
  89.                     4: (*right*)
  90.                         begin
  91.                             him^.position.h := him^.position.h + diff[4] + 1;
  92.                             if him^.speed.h < 0 then
  93.                                 him^.speed.h := -him^.speed.h;
  94.                         end;
  95.                 end; (* switch *)
  96.             end;  (* if *)
  97.     end;
  98.  
  99.     procedure SetupHMovPlatform (me: SpritePtr);
  100.         var
  101.             r: Rect;
  102.             pol: PolyHandle;
  103.     begin
  104.         me^.speed.h := -1 + SATRand(2) * 2;
  105.         me^.face := platFace;
  106.         me^.appPtr := NewPtr(sizeof(MovPlatRec));
  107.         if me^.appPtr <> nil then
  108.             begin
  109.                 MovPlatP(me^.appPtr)^.MaxV := me^.position.h;
  110.                 MovPlatP(me^.appPtr)^.MinV := me^.kind;
  111.             end;
  112.         SetRect(me^.hotRect, 0, 3, 60, 20);
  113.         me^.task := @HandleHMovPlatform;
  114.         me^.hitTask := @HitHMovPlatform;
  115.     end;
  116.  
  117. end.